//
//  PlanRecommendCell.m
//  WanCaiC8
//
//  Created by tangang on 2018/3/23.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "PlanRecommendCell.h"
#import "LabCell.h"
@interface PlanRecommendCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic ,strong) UICollectionView *collectionView;
@end

@implementation PlanRecommendCell{
    NSMutableArray *arrData;
    NSMutableArray *arrResult;
    UIColor *bgColor;
    QXLable *labName;
    LotteryNumType   lType;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
//        self.backgroundColor=DEFAULTBGCOLOR;
        labName=[[QXLable alloc]init];
        labName.textAlignment=NSTextAlignmentCenter;
        [self.contentView addSubview:labName];
        [labName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(self.contentView);
            make.width.mas_equalTo(@(SCREEN_WIDTH/4.0));
        }];
        
        UIView  *rightLine=[[UIView alloc]init];
        rightLine.backgroundColor=DEFAULTLINECOLOR;
        [self.contentView addSubview:rightLine];
        [rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.equalTo(labName);
            make.width.mas_equalTo(@.5);
        }];
        
        UIView *bottomLine=[[UIView alloc]init];
        bottomLine.backgroundColor=DEFAULTLINECOLOR;
        [self.contentView addSubview:bottomLine];
        [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(labName);
            make.height.mas_equalTo(@.7);
        }];
        
        [self.contentView addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.top.right.equalTo(self.contentView);
            make.left.equalTo(labName.mas_right);
            make.height.mas_equalTo(@(real_size(35)));
        }];
    }
    return self;
}


- (void)setModel:(PlanRecommendModel *)model{
    lType=[[BaseVM manage] getType:model.lType];
    if (lType==LotteryNumTypeHappy12||lType==LotteryNumType11Select5||lType==LotteryNumTypeLuckyAirship||lType==LotteryNumTypeCarPK10) {
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@(real_size(70)));
        }];
    }else if (lType==LotteryNumTypeHappy10){
//        if (model.ShowSort.length==0) {
//            model.ShowSort=@"总和";
//        }
        if ([model.ShowSort isEqualToString:@"总和"]) {
            [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(@(real_size(35)));
            }];
        }else{
            [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(@(real_size(70)));
            }];
        }
        
    }else if (lType==LotteryNumTypeLottery&&[model.ShowSort isEqualToString:@"二十码中特"]){
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@(real_size(70)));
        }];
    }
    else{
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@(real_size(35)));
        }];
    }
    labName.text=model.ShowSort;
    _model=model;
    arrData=[[model.Num componentsSeparatedByString:@"|"] mutableCopy];
    arrResult=[[model.Result componentsSeparatedByString:@"|"] mutableCopy];
    if (lType==LotteryNumTypeHappy10&&arrData.count>4) {
//        NSMutableArray *arr=[arrData mutableCopy];
        NSString *str=arrData[2];
        [arrData replaceObjectAtIndex:2 withObject:[NSString stringWithFormat:@"合%@",str]];
        NSString *str1=arrData[3];
        [arrData replaceObjectAtIndex:3 withObject:[NSString stringWithFormat:@"尾%@",str1]];
        [arrResult removeObjectAtIndex:arrResult.count-2];
    }
    if (lType!=LotteryNumTypeLottery) {
        [arrData removeObject:@"龙"];
        [arrData removeObject:@"虎"];
    }
    
    if (arrData.count<arrResult.count) {
        [arrResult removeLastObject];
    }
    [self.collectionView reloadData];
}

- (void)setIsNew:(BOOL)isNew{
    _isNew=isNew;
    [self.collectionView reloadData];
}

#pragma mark - collectionView代理函数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return arrData.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LabCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"LabCell" forIndexPath:indexPath];
    cell.labTitle.attributedText=nil;
    cell.labTitle.textColor=DEFAULTTEXTBLACKCOLOR;
    [cell setLeftTitle:@"" andLeft:0];
    cell.labTitle.textAlignment=NSTextAlignmentCenter;
    cell.labTitle.text=nil;
    NSString *Result;
    if (arrResult.count>indexPath.row) {
        Result=arrResult[indexPath.row];
    }
    NSString *title;
    if (arrData.count>indexPath.row) {
        title=arrData[indexPath.row];
    }
    if (self.isNew) {
        cell.labTitle.text=title;
    }else{
        if ([Result isEqualToString:@"中"]) {
            title=[NSString stringWithFormat:@"%@(中)",title];
        }
        NSArray *resultArr;
        if (indexPath.row==0) {
            resultArr=[arrData[0] componentsSeparatedByString:@" "];
        }
        cell.labTitle.attributedText=nil;
        cell.labTitle.text=nil;
        if ([Result isEqualToString:@"中"]) {
            NSMutableAttributedString *attStr=[self attStrWithResultArr:resultArr andTitle:title];
            NSRange rang=[title rangeOfString:@"(中)"];
            [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
            cell.labTitle.attributedText=attStr;
        }else{
            cell.labTitle.text=title;
        }
    }
    return cell;
}

- (NSMutableAttributedString *)attStrWithResultArr:(NSArray *)resultArr andTitle:(NSString *)title{
    NSMutableAttributedString *attStr=[[NSMutableAttributedString alloc]initWithString:title];
    NSString *strChise=[title stringByReplacingOccurrencesOfString:@"(中)" withString:@""];
    if ([self isChinese:strChise]) {
        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:NSMakeRange(0, attStr.length)];
        return attStr;
    }

    LotteryNumType type=[[BaseVM manage]getType:self.model.lType];
    if (type==LotteryNumTypeCarPK10||type==LotteryNumTypeLottery3D||type==LotteryNumType11Select5||type==LotteryNumTypeHappy10||type==LotteryNumTypeArrangement3||type==LotteryNumTypeArrangement5||type==LotteryNumTypeSevenStars||type==LotteryNumTypeAllTime||type==LotteryNumTypeHappy12||type==LotteryNumTypeLuckyAirship) {
        NSArray *arrNum=[self.model.OpenNum componentsSeparatedByString:@","];
        if (self.row<arrNum.count) {
            NSString *strNum=arrNum[self.row];
            for (NSString *str in resultArr) {
                if ([str isEqualToString:strNum]) {
                    NSRange rang=[title rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    break;
                }
            }
        }
    }else if (type==LotteryNumTypeNearlyThree){
        NSArray *arrNum=[self.model.OpenNum componentsSeparatedByString:@","];
        if ([self.model.ShowSort isEqualToString:@"独胆"]||[self.model.ShowSort isEqualToString:@"双胆"]) {
            for (NSString *strNum in arrNum) {
                for (NSString *str in resultArr) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[title rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([self.model.ShowSort isEqualToString:@"和值"]){
            int sum=0;
            for (NSString *strNum in arrNum) {
                sum+=[strNum intValue];
            }
            NSRange rang=[title rangeOfString:[NSString stringWithFormat:@"%d",sum]];
            [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
            return attStr;
        }
        
    }else if (type==LotteryNumTypeBrings){
        if ([self.model.ShowSort isEqualToString:@"前区4码"]) {
            NSArray *arrNum=[self.model.OpenNum componentsSeparatedByString:@","];
            for (int i=0; i<arrNum.count-2; i++) {
                NSString *strNum=arrNum[i];
                for (NSString *str in resultArr) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[title rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    }
                }
            }
        }else if ([self.model.ShowSort isEqualToString:@"后区3码"]){
            NSArray *arrNum=[self.model.OpenNum componentsSeparatedByString:@","];
            if (arrNum.count!=7) {
                return attStr;
            }
            for (NSString *str in resultArr) {
                for (int i=5;i<arrNum.count;i++) {
                    NSString *strNum=arrNum[i];
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[title rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }
        return attStr;
    }else if (type==LotteryNumTypeLottery){
        if ([self.model.ShowSort isEqualToString:@"平五码"]) {
            NSArray *arrNum=[self.model.OpenNum componentsSeparatedByString:@","];
            for (int i=0; i<arrNum.count-1; i++) {
                NSString *strNum=arrNum[i];
                for (NSString *str in resultArr) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[title rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    }
                }
            }
        }else if ([self.model.ShowSort isEqualToString:@"六肖中特"]){
            NSArray *arrNum=[self.model.OpenNumAlias componentsSeparatedByString:@","];
            NSString *strNum=arrNum.lastObject;
            for (NSString *str in resultArr) {
                if ([str isEqualToString:strNum]) {
                    NSRange rang=[title rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                }
            }
        }else if ([self.model.ShowSort isEqualToString:@"二十码中特"]){
            NSArray *arrNum=[self.model.OpenNum componentsSeparatedByString:@","];
            NSString *strNum=arrNum.lastObject;
            for (NSString *str in resultArr) {
                if ([str isEqualToString:strNum]) {
                    NSRange rang=[title rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    break;
                }
            }
        }
    }else if (type==LotteryNumTypeSevenLecai){
        if ([self.model.ShowSort isEqualToString:@"胆一码"]||[self.model.ShowSort isEqualToString:@"胆二码"]||[self.model.ShowSort isEqualToString:@"胆三码"]) {
            NSArray *arrNum=[self.model.OpenNum componentsSeparatedByString:@","];
            for (int i=0; i<arrNum.count; i++) {
                NSString *strNum=arrNum[i];
                for (NSString *str in resultArr) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[title rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    }
                }
            }
        }
        return attStr;
    }else if (type==LotteryNumTypeChromosphere){
        if ([self.model.ShowSort isEqualToString:@"红3胆"]||[self.model.ShowSort isEqualToString:@"红2胆"]) {
            NSArray *arrNum=[self.model.OpenNum componentsSeparatedByString:@","];
            for (int i=0; i<arrNum.count-1; i++) {
                NSString *strNum=arrNum[i];
                for (NSString *str in resultArr) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[title rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    }
                }
            }
        }else if ([self.model.ShowSort isEqualToString:@"蓝8码"]){
            NSArray *arrNum=[self.model.OpenNum componentsSeparatedByString:@","];
            for (NSString *str in resultArr) {
                NSString *strNum=arrNum.lastObject;
                if ([str isEqualToString:strNum]) {
                    NSRange rang=[title rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    break;
                }
            }
        }
        return attStr;
    }
    
    
    return attStr;
}

- (BOOL)isChinese:(NSString *)str
{
    NSString *match = @"(^[\u4e00-\u9fa5]+$)";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
    return [predicate evaluateWithObject:str];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    float height=35;
    if (lType==LotteryNumTypeHappy12||lType==LotteryNumType11Select5||lType==LotteryNumTypeCarPK10||lType==LotteryNumTypeLuckyAirship) {
        height=70;
    }else if (lType==LotteryNumTypeLottery&&[self.model.ShowSort isEqualToString:@"二十码中特"]){
        height=70;
    }
    if (lType==LotteryNumTypeLottery) {
        return CGSizeMake((SCREEN_WIDTH-(SCREEN_WIDTH/4.0)), real_size(height));
    }else if (lType==LotteryNumTypeChromosphere){
        return CGSizeMake((SCREEN_WIDTH-(SCREEN_WIDTH/4.0)), real_size(height));
    }else if (lType==LotteryNumTypeHappy10){
        if (arrData.count==2) {
            return CGSizeMake(floor((SCREEN_WIDTH-(SCREEN_WIDTH/4.0))/2.0), real_size(35));
        }
        if (indexPath.row==0) {
            return CGSizeMake((SCREEN_WIDTH-(SCREEN_WIDTH/4.0)), real_size(35));
        }else{
            return CGSizeMake(floor((SCREEN_WIDTH/4.0*3)/(arrData.count-1)), real_size(35));
        }
    }
    else if (lType==LotteryNumTypeCarPK10||lType==LotteryNumTypeLuckyAirship){
        if (indexPath.row==0) {
            return CGSizeMake((SCREEN_WIDTH-(SCREEN_WIDTH/4.0)), real_size(35));
        }else{
            return CGSizeMake(floor((SCREEN_WIDTH/4.0*3)/(arrData.count-1)), real_size(35));
        }
    }else if (lType==LotteryNumType11Select5){
        if (arrData.count>2) {
            if (indexPath.row==0) {
                return CGSizeMake(floor((SCREEN_WIDTH-(SCREEN_WIDTH/4.0))/2), real_size(height));
            }else{
                return CGSizeMake(floor((SCREEN_WIDTH-(SCREEN_WIDTH/4.0))/4), real_size(height));
            }
        }
        
    }
    return CGSizeMake(floor((SCREEN_WIDTH/4.0*3)/arrData.count), real_size(height));
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)setBgColor:(UIColor *)color{
    self.contentView.backgroundColor=color;
}

#pragma mark -- 懒加载
- (UICollectionView *)collectionView{
    if (_collectionView==nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake(SCREEN_WIDTH/4.0, real_size(35))];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 0.0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor=[UIColor clearColor];
        _collectionView.showsVerticalScrollIndicator=NO;
        [_collectionView registerClass:[LabCell class] forCellWithReuseIdentifier:@"LabCell"];
        
    }
    return _collectionView;
}

@end
